﻿using System.Collections.Generic;
using System.Windows.Forms;
using ApplicationLibrary.ExtendedControls;
using ApplicationLibrary.SqlConnector;

namespace Clothes.Items.ModuleData
{
    internal class RadarCategoriesParam : RadarParameters
    {
        public RadarCategoriesParam(string code)
        {
            Params = new List<object>();
            Params.Add(code);
        }
    }

    internal class RadarItemsParam : RadarParameters
    {
        public RadarItemsParam(int code)
        {
            Params = new List<object>();
            Params.Add(code);
        }
    }

    #region Radar Categories

    internal class RadarCategories : RadarForm
    {
        private QueryBuilder qb = new QueryBuilder();
        private SqlABParameter p1;

        public RadarCategories()
            : base()
        {
            rdrCodeColumn = CL_Categories.Code.Name;
            rdrDescColumn = CL_Categories.Description.Name;
            rdrNameSpace = "Clothes.Clothes.Items.Forms.Categories";
        }

        protected override Table GetRadarTable()
        {
            return new CL_Categories();
        }

        protected override bool DefineFindQuery(SqlABCommand sqlCmd)
        {
            p1 = new SqlABParameter("@p1", CL_Categories.Code);

            qb.Clear();
            qb.AddSelectAll(CL_Categories.Name);
            qb.AddFrom(CL_Categories.Name);
            qb.AddCompare(CL_Categories.Code, p1);

            sqlCmd.CommandText = qb.Query;
            sqlCmd.Parameters.Add(p1);

            return true;
        }

        protected override void PrepareFindQuery(IRadarParameters param)
        {
            p1.Value = param.Params[0];
        }

        protected override void OnFound(SqlABDataReader sqlReader)
        {
            Description = sqlReader[CL_Categories.Description.Name].ToString();
        }

        protected override string DefineBrowseQuery(SqlABCommand sqlCmd, string findQuery)
        {
            qb.Clear();
            qb.AddSelectAll(CL_Categories.Name);
            qb.AddFrom(CL_Categories.Name);

            qb.AddFilter(findQuery);

            return qb.Query;
        }

        protected override void PrepareBrowseQuery()
        {
        }

        protected override IRadarParameters PrepareRadarParameters(DataGridViewRow row)
        {
            return new RadarCategoriesParam(DataGridViewRowValues.GetValue<string>(row, CL_Categories.Code));
        }

        public override IRadarParameters GetRadarParameters(string text)
        {
            return new RadarCategoriesParam(text);
        }

        public override string GetCodeFromParameters(IRadarParameters param)
        {
            return param.GetValue<string>(1);
        }

        //protected override void FormatColumns(DataGridViewRow row, string columnName)
        //{
        //    if (columnName == FG_Athlete.Club.Name && row.Cells[0].Value != null)
        //    {
        //        string mail = DataGridViewRowValues.GetValue<string>(row, FG_Athlete.Mail);
        //        if (DRClub.Find(mail))
        //            DataGridViewRowValues.SetValue<string>(row, FG_Athlete.Club, DRClub.GetValue<string>(FG_Club.Club));
        //    }
        //}

    }

    #endregion

    #region Radar Items

    internal class RadarItems : RadarForm
    {
        private QueryBuilder qb = new QueryBuilder();
        private SqlABParameter p1;

        public RadarItems()
            : base()
        {
            rdrCodeColumn = CL_Items.ID.Name;
            rdrDescColumn = CL_Items.Description.Name;
            rdrNameSpace = "Clothes.Clothes.Items.Forms.Items";
        }

        protected override Table GetRadarTable()
        {
            return new CL_Items();
        }

        protected override bool DefineFindQuery(SqlABCommand sqlCmd)
        {
            p1 = new SqlABParameter("@p1", CL_Items.ID);

            qb.Clear();
            qb.AddSelectAll(CL_Items.Name);
            qb.AddFrom(CL_Items.Name);
            qb.AddCompare(CL_Items.ID, p1);

            sqlCmd.CommandText = qb.Query;
            sqlCmd.Parameters.Add(p1);

            return true;
        }

        protected override void PrepareFindQuery(IRadarParameters param)
        {
            p1.Value = param.Params[0];
        }

        protected override void OnFound(SqlABDataReader sqlReader)
        {
            Description = sqlReader[CL_Items.Description.Name].ToString();
        }

        protected override string DefineBrowseQuery(SqlABCommand sqlCmd, string findQuery)
        {
            qb.Clear();
            qb.AddSelectAll(CL_Items.Name);
            qb.AddFrom(CL_Items.Name);

            qb.AddFilter(findQuery);

            return qb.Query;
        }

        protected override void PrepareBrowseQuery()
        {
        }

        protected override IRadarParameters PrepareRadarParameters(DataGridViewRow row)
        {
            return new RadarItemsParam(DataGridViewRowValues.GetValue<int>(row, CL_Items.ID));
        }

        public override IRadarParameters GetRadarParameters(string text)
        {
            return new RadarItemsParam(int.Parse(text));
        }

        public override string GetCodeFromParameters(IRadarParameters param)
        {
            return param.GetValue<string>(1);
        }

        //protected override void FormatColumns(DataGridViewRow row, string columnName)
        //{
        //    if (columnName == FG_Athlete.Club.Name && row.Cells[0].Value != null)
        //    {
        //        string mail = DataGridViewRowValues.GetValue<string>(row, FG_Athlete.Mail);
        //        if (DRClub.Find(mail))
        //            DataGridViewRowValues.SetValue<string>(row, FG_Athlete.Club, DRClub.GetValue<string>(FG_Club.Club));
        //    }
        //}

    }

    #endregion
}